package org.leaf.green.b2b

import org.springframework.transaction.annotation.*
import org.hibernate.FetchMode as FM

class DistributionAjaxService {

    static transactional = true
    //get staff list
    @Transactional(readOnly = true)
    def staffList(term) {
        log.info("staffList")
        def staffList = Staff.withCriteria{
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)
            order("serialNumber", "asc")
        }

        return staffList
    }

    //get staff list
    @Transactional(readOnly = true)
    def retailerList(term) {
        log.info("retailerList")
        def retailerList = Retailer.withCriteria{
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)
            order("serialNumber", "asc")
        }

        return retailerList
    }

    //get packages list
    @Transactional(readOnly = true)
    def packageList(term) {
        log.info("packageList")
        def packageList = Package.withCriteria{
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)
            order("serialNumber", "asc")
        }

        return packageList
    }

}
